package sg.edu.nus.iss.billing.gui;

//Tan Pic Cheng

	import java.awt.BorderLayout;
	import java.awt.Checkbox;
	import java.awt.CheckboxGroup;
	import java.awt.Dimension;
	import java.awt.FlowLayout;
	import java.awt.Font;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;


	import javax.swing.JButton;
	import javax.swing.JLabel;
	import javax.swing.JOptionPane;
	import javax.swing.JPanel;
	import javax.swing.JTabbedPane;
	import javax.swing.SwingConstants;

	import sg.edu.nus.iss.billing.controller.CustomerController;
	import sg.edu.nus.iss.billing.entity.Customer;
	import sg.edu.nus.iss.billing.entity.RecordState;
	import sg.edu.nus.iss.billing.exception.ControllerException;
	import sg.edu.nus.iss.billing.exception.ValidationException;
	import sg.edu.nus.iss.billing.gui.component.ErrorDialog;
	import sg.edu.nus.iss.billing.gui.component.ValidateJTextField;
	import sg.edu.nus.iss.billing.localization.MessageKeyManager;
import sg.edu.nus.iss.billing.validation.ValidationRule;

	public class DeregisterCustomerPanel extends DefaultFunctionPanel  {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private static int HEIGHT = 30;		
		private static int LABELWIDTH = 200;
		private String fieldCustNRICUpper;
			
		private JLabel lblShowCustID;
		private JLabel lblShowCustNRIC;
		private JLabel lblShowCustName;
		private JLabel lblCustID;
		private JLabel lblCustNRIC;
		private	JLabel lblCustName;
		private JLabel lblWarning = new JLabel();
		private JLabel lblCustHeader;		
		private JLabel lblCustDetails;
		private CheckboxGroup cbg;
		private Checkbox ckNRIC;
		private Checkbox ckID;
			
		private Boolean ckState;
		
		private ValidateJTextField fieldCustNRIC;
		
		private JButton lookupButton;
		private JButton deregisterButton;
		
		private		JPanel 		tabPanel;	
		private		JTabbedPane tabbedPane;
		private		JPanel		deregisterCustPanel;
		private 	JPanel 		searchPanel;
		private		JPanel		actionPanel;
		
			
		//4 panels to view customer's personal information
		private ViewCustomerFieldPanel custPanel;
		private ViewCustomerAddressPanel custAdd;
		private ViewCustomerContactPanel custContact;
		private ViewCustomerMiscPanel custMisc;
		
		private CustomerController customerController;
		private Customer cust;
				
			
		public DeregisterCustomerPanel(){
			
			customerController = new CustomerController();
			super.init("TOPONLY");
			
		}	
		public JPanel createTopPanel(){
		 //Specify the top panel to display NRIC & Customer Account Number search options	 
			searchPanel = new JPanel(null);
		 	 	
		 	lblCustHeader = new JLabel(MessageKeyManager.getMessage("label.custHeader"));
		 	Font f = new Font("Arial",Font.BOLD, 12);
		 	lblCustHeader.setFont(f);
		 	lblCustHeader.setBounds(10, 15, 200, HEIGHT);
		 	searchPanel.add(lblCustHeader);
		 	
		 	JPanel displayOptions = displaySearchOptions();
		 	displayOptions.setBounds(0, 25, 800, 80);
		 	searchPanel.add(displayOptions);
		 	return searchPanel;
		 		  
		}
		private JPanel displaySearchOptions() {
			// TODO Auto-generated method stub
			JPanel p = new JPanel(null);
			p.setPreferredSize(new Dimension(800, 80));
			
			lblCustDetails = new JLabel(MessageKeyManager.getMessage("label.custDetails"));
			lblCustDetails.setBounds(10, 15, 150, HEIGHT);
			p.add(lblCustDetails);
			
			cbg = new CheckboxGroup();
			ckNRIC = new Checkbox(MessageKeyManager.getMessage("label.nric"), true, cbg);
			ckID = new Checkbox(MessageKeyManager.getMessage("label.custAcct"), false, cbg);
			ckNRIC.setBounds(180, 15, 250, HEIGHT);
			ckID.setBounds(450, 15, 250, HEIGHT);
			
			p.add(ckNRIC);
			p.add(ckID);
				
			fieldCustNRIC = new ValidateJTextField(MessageKeyManager.getMessage("label.custDetails"));
			fieldCustNRIC.setBounds(180, 50, 250, 25);
			p.add(fieldCustNRIC);
			
			lookupButton = new JButton(MessageKeyManager.getMessage("button.lookup"));
			lookupButton.setBounds(450, 50, 100, 25);
			lookupButton.addActionListener(new lookupListener());
			p.add(lookupButton);
						
			return p;
		}
		public JPanel createBottomPanel(){
				tabPanel = new JPanel();
												
				// Create a tabbed pane for de-registering of existing customer
				tabbedPane = new JTabbedPane();
				tabbedPane.setPreferredSize(new Dimension(800,600));
				deregisterCustPanel = deregisterCustomerPanel();
				tabbedPane.addTab( "De-register Customer", deregisterCustPanel );
				
				lblShowCustID.setText(cust.getAccountNumber());
				lblShowCustNRIC.setText(cust.getNRIC());
				lblShowCustName.setText(cust.getName());
				
				tabPanel.add(tabbedPane);
				
				return tabPanel;
				
		}
		public JPanel deregisterCustomerPanel()
		{	
		 	JPanel mainPanel = new JPanel(null);
		 	mainPanel.setBounds(0, 0, 800, 600);		 	
		 						
			lblCustID = new JLabel(MessageKeyManager.getMessage("label.custAcct"));
			lblCustID.setBounds(10, 5, LABELWIDTH, HEIGHT);
			mainPanel.add( lblCustID );
			
			lblCustNRIC = new JLabel(MessageKeyManager.getMessage("label.nric"));
			lblCustNRIC.setBounds(10, 40, LABELWIDTH, HEIGHT);
			mainPanel.add( lblCustNRIC );
			
			lblCustName = new JLabel(MessageKeyManager.getMessage("label.custName"));
			lblCustName.setBounds(10, 75, LABELWIDTH, HEIGHT);
			mainPanel.add( lblCustName );
			
			lblShowCustID = new JLabel();
			lblShowCustID.setBounds(250, 5, LABELWIDTH, HEIGHT);
			mainPanel.add( lblShowCustID );
			
			lblShowCustNRIC = new JLabel();
			lblShowCustNRIC.setBounds(250, 40, LABELWIDTH, HEIGHT);
			mainPanel.add( lblShowCustNRIC );
			
			lblShowCustName = new JLabel();
			lblShowCustName.setBounds(250, 75, LABELWIDTH, HEIGHT);
			mainPanel.add( lblShowCustName );
										
			return mainPanel;
		}
	 	public JPanel createActionButtonsPanel(){
			
		 	actionPanel = new JPanel();
		 	actionPanel.setLayout(new FlowLayout());
		 	deregisterButton = new JButton(MessageKeyManager.getMessage("button.deregister"));
		 	deregisterButton.addActionListener(new DeregisterActionListener());
		 	
		 	actionPanel.add(deregisterButton);
		 	return actionPanel;
		}
	 	private class DeregisterActionListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				try {					
					customerController.deleteCustomer(cust);
		            JOptionPane.showMessageDialog(null, "Customer has been deleted", "Delete Customer Success", JOptionPane.INFORMATION_MESSAGE);
		            tabPanel.removeAll();
		            actionPanel.removeAll();
		            tabPanel.updateUI();
		            actionPanel.updateUI();
				}catch (ControllerException ex) {
					ErrorDialog.showControllerErrorMessageDialog(ex);
				 }catch (Exception ex) {
					ErrorDialog.showUnhandledErrorMessageDialog(ex);
				}
			}
		}	
	 	public Customer loadCustomerByNRIC(String nric) {
			
			Customer custObj = null;
			
			try {
				
				custObj = customerController.isNRICExist(nric);		
				
			} catch (ControllerException ex){	
				
				ex.printStackTrace();	
				
			}
			
			return custObj;
			
		}
		public Customer loadCustomerByID(String id){
			
			Customer custObj = null;
			
			try {
							
				custObj = customerController.getCustomerByAccount(id);
				
			} catch (ControllerException ex){
				
				ex.printStackTrace();	
				
			}
			
			return custObj;
			
		}
		public class lookupListener implements ActionListener{
			private String fieldCustNRICUpper;

			@Override
				public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				

				Checkbox selectedCheckbox;
				selectedCheckbox = cbg.getSelectedCheckbox();
				
				if(selectedCheckbox == ckNRIC)
					ckState = true;
				else
					ckState = false;
				
					try { 
							if(tabPanel != null){
								tabPanel.removeAll();
								tabPanel.updateUI();
							}
							if(actionPanel != null){
								actionPanel.removeAll();
								actionPanel.updateUI();
							}
							if(lblWarning.isShowing() == true){
							searchPanel.remove(lblWarning);
							searchPanel.updateUI();		
							}
							fieldCustNRICUpper = fieldCustNRIC.getText().toUpperCase().trim();
							fieldCustNRIC.setText(fieldCustNRICUpper);
						
						if(ckState == true){					
							fieldCustNRIC.setRules(ValidationRule.NOT_NULL, ValidationRule.NRIC);
							cust = loadCustomerByNRIC(fieldCustNRIC.getValidatedText());
									if (cust == null ) {							
									String msg = "Customer with NRIC: " + fieldCustNRIC.getText() + " is a new customer!";
									lblWarning.setText(msg);
									lblWarning.setBounds(180, 100, 300, 20);
									lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);
									searchPanel.add(lblWarning);
									searchPanel.updateUI();
									}else if(cust != null && cust.getActive() == RecordState.DELETED){
										SessionContext.setCustomer(cust);
										String msg = "Customer with NRIC: " + fieldCustNRIC.getText() + " - Record has already been deleted!";
										lblWarning.setText(msg);
										lblWarning.setBounds(180, 100, 550, 20);
										lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);
										searchPanel.add(lblWarning);
										searchPanel.updateUI();	
									}else{	 
										  SessionContext.setCustomer(cust);
										  refreshBottomPanel(createBottomPanel());
										  refreshActionButtonPanel(createActionButtonsPanel());	
									}
						}		
						else{
								fieldCustNRIC.setRules(ValidationRule.NOT_NULL, ValidationRule.ACCOUNT_NUMBER);
								cust = loadCustomerByID(fieldCustNRIC.getValidatedText());
								
								if(cust == null){
									String msg = "Customer with Customer Account Number: " + fieldCustNRIC.getText() + " is a new customer!";
									
									lblWarning = new JLabel(msg);
									lblWarning.setBounds(180, 100, 500, 20);
									lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);

									searchPanel.add(lblWarning);
									searchPanel.updateUI();								
								}else if(cust != null && cust.getActive() == RecordState.DELETED){
									String msg = "Customer with Customer Account Number: " + fieldCustNRIC.getText() + " - Record has already been deleted!";
									lblWarning.setText(msg);
									lblWarning.setBounds(100, 100, 550, 20);
									lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);
									searchPanel.add(lblWarning);
									searchPanel.updateUI();	
								}else{
									  refreshBottomPanel(createBottomPanel());	
									  refreshActionButtonPanel(createActionButtonsPanel());	
								}
						}	
						 fieldCustNRIC.setText("");	
					}catch(ValidationException ex) {				
						ErrorDialog.showValidationErrorMessageDialog(ex);
					}			
				}
			}		
	 				
	}



